<?php
class Familia extends AppModel{
    
    var $name = 'Familia';    
       
    var $hasMany = array(
            'MiembroFamiliare' => array(
            'className' => 'MiembroFamiliare',
            'foreignKey' => 'familia_id'
            )
      );
     
    var $belongsTo = array(            
            'Alumno'=> array(
            'className' => 'Alumno',
            'foreignKey' => 'alumno_id'
            ),
      );
    
    function generarCodigo(){          
        $this->recursive = -1;
        $codigo = $this->find('first', array(
                         'fields' => array('MAX(codigo) as "Familia__codigo"'),
                         'conditions'=>array('activo'=>true)
                         )
                  );

        if(!empty($codigo['Familia']['codigo'])){
            $code = str_pad((int)$codigo['Familia']['codigo'] + 1 ,4,'0',STR_PAD_LEFT); 
        }
        else{
            $code = str_pad('1',5,'0',STR_PAD_LEFT);          
        }
        return $code;
    }
    
}
?>